package com.zengqingfa.basic.collection;

import java.util.ArrayList;
import java.util.List;

/**
 * @fileName: ArrayListConcurrent
 * @author: zengqf3
 * @date: 2021-6-9 14:55
 * @description:
 */
public class ArrayListConcurrent {
    public static List<Integer> numberlist = new ArrayList<>();

    class AddToTest implements Runnable {

        int startNum = 0;

        public AddToTest(int startNum) {
            this.startNum = startNum;
        }

        @Override
        public void run() {
            int count = 0;
            while (count < 100) {

                try {
                    java.lang.Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                numberlist.add(startNum);
                System.out.println(java.lang.Thread.currentThread().getName() + "--" + (count) + "times"
                        +" 被添加的数字："+ startNum
                        + ",list的最后数字:" + numberlist.get(numberlist.size() - 1)
                        + ",集合的大小：" + numberlist.size()
                        + ",list" + numberlist.toString());

                startNum += 2;
                count++;
            }
        }
    }

    public static void main(String[] args) {
        Thread t = new Thread(new ArrayListConcurrent().new AddToTest(0));
        Thread t1 = new Thread(new ArrayListConcurrent().new AddToTest(100));
        t.start();
        t1.start();
    }

}
